﻿using System.Windows.Controls;
using System.Windows.Navigation;
using DeepEarth.Map.Core;
using DeepEarth.Core.Data;
using DeepEarth.Toolkit.Geometry;
using System.Windows.Media;
using DeepEarth.Core;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System;
using ExampleApplication.Utilities;
using System.Windows.Threading;

namespace ExampleApplication.Examples
{
    public partial class ScratchPad : Page
    {
        public ScratchPad()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            AddOnTimer();
        }

        Random rng;
        DispatcherTimer timer;
        VectorLayer vl;
        int items;

        void AddOnTimer()
        {
            vl = new VectorLayer();
            this.map.BaseChildren.Add(vl);

            rng = new Random(1);
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (items % 7 == 0)
            {
                Polygon p = Utilities.Utilities.RandomPolygon(rng, rng.Next(10));
                p.Stroke = Utilities.Utilities.RandomStroke(rng);
                p.StrokeThickness = rng.Next(1, 4);
                p.Fill = Utilities.Utilities.RandomStroke(rng);
                vl.Add(p.BoundingBox, p);
            }
            else
            { 
                LineString ls = Utilities.Utilities.RandomLine(rng, 2);
                ls.Stroke = Utilities.Utilities.RandomStroke(rng);
                ls.StrokeThickness = rng.Next(1, 4);
                vl.Add(ls.BoundingBox, ls);
            }

            if (items++ > 1000)
            {
                timer.Stop();
            }
        }

        void AddStuff()
        {
            vl = new VectorLayer();
            this.map.BaseChildren.Add(vl);

            Random rng = new Random(1);

            for (int i = 0; i < 1000; i++)
            {
                LineString ls = Utilities.Utilities.RandomLine(rng, 2);
                ls.Stroke = Utilities.Utilities.RandomStroke(rng);
                ls.StrokeThickness = rng.Next(1, 4);
                vl.Add(ls.BoundingBox, ls);
            }
        }
    }
}
